﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace Housing
{
    public class Applicant : System.Web.UI.Page
    {
        private int _app_id;
        private int _app_applyid;
        private int _app_noid;
        private string _app_title;
        private string _app_name;
        private string _app_marryed;
        private string _app_workunit;
        private string _app_idcard;
        private float _app_income;
        private short _app_ispro;
        private int _effectDate;
        public int EffectDate
        {
            get { return _effectDate; }
            set { _effectDate = value; }
        }

        public int app_id
        {
            get { return _app_id; }
        }

        public int app_applyid
        {
            get { return _app_applyid; }
            set { _app_applyid = value; }
        }

        public int app_noid
        {
            get { return _app_noid; }
            set { _app_noid = value; }
        }

        public string app_title
        {
            get { return _app_title; }
            set { _app_title = value; }
        }

        public string app_name
        {
            get { return _app_name; }
            set { _app_name = value; }
        }

        public string app_marryed
        {
            get { return _app_marryed; }
            set { _app_marryed = value; }
        }

        public string app_workunit
        {
            get { return _app_workunit; }
            set { _app_workunit = value; }
        }

        public string app_idcard
        {
            get { return _app_idcard; }
            set { _app_idcard = value; }
        }

        public float app_income
        {
            get { return _app_income; }
            set { _app_income = value; }
        }

        public short app_ispro
        {
            get { return _app_ispro; }
            set { _app_ispro = value; }
        }


        public Applicant()
        {
        }

        public Applicant(int appId)
        {
            GetApplicantByAppId(appId);
        }

        public void GetApplicantByAppId(int appId)
        {
            string query = "SELECT * FROM Applicant WHERE app_id=" + appId;

            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionString,
                CommandType.Text, query);

            if (dr.Read())
            {
                _app_id = (int)dr[0];
                _app_applyid = Utils.GetNullableInt(dr[1]);
                _app_noid = Utils.GetNullableInt(dr[2]);
                _app_title = Utils.GetString(dr[3]);
                _app_name = Utils.GetString(dr[4]);
                _app_marryed = Utils.GetString(dr[5]);
                _app_workunit = Utils.GetString(dr[6]);
                _app_idcard = Utils.GetString(dr[7]);
                _app_income = Utils.GetNullableFloat(dr[8]);
                _app_ispro = (short)Utils.GetNullableInt(dr[9]);
            }

            dr.Close();
        }

        public DataSet GetAllApplicant()
        {
            string query = "SELECT * FROM Applicant";

            return SqlHelper.ExecuteDataset(SqlHelper.ConnectionString, CommandType.Text, query);
        }

        public int AddNewApplicant(Applicant app)
        {
            //修改：app.app_marryed 前后加引号
            string query = "INSERT INTO Applicant(app_applyid, app_noid, app_title, app_name, app_marryed,"
                           + "app_workunit, app_idcard, app_income, app_ispro,EffectDate)"
                           + "VALUES(" + app.app_applyid + "," + app.app_noid + ",'" + app.app_title + "','" + app.app_name + "','"
                           + app.app_marryed + "','" + app.app_workunit + "','" + app.app_idcard + "'," + app.app_income + "," + app.app_ispro + "," + app.EffectDate + ")";

           // Response.Write(query);
            //Response.End();
            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, query);
        }

        public int UpdateApplicantByAppId(Applicant app)
        {
            string query = "UPDATE Applicant SET app_applyid=" + app.app_applyid + ", app_noid=" + app.app_noid + ","
                           + "app_title='" + app.app_title + "',app_name='" + app.app_name + "',"
                           + "app_marryed='" + app.app_marryed + "',app_workunit='" + app.app_workunit + "',"
                           + "app_idcard='" + app.app_idcard + "',app_income=" + app.app_income + ",app_ispro=" + app.app_ispro
                           + "WHERE app_id=" + app.app_id;

            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, query);
        }

        public int DeleteApplicantByAppId(int appId)
        {
            string query = "DELETE FROM Applicant WHERE app_Id=" + appId;

            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, query);
        }

        public int DeleteApplicantByAppApplyid(string applyid)
        {
            string query = "DELETE FROM Applicant WHERE app_applyid=" + applyid;

            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, query);
        }

        public SqlDataReader GetApplicantByApplyid(string applyid)
        {
            string query = "SELECT * FROM Applicant WHERE app_applyid='" + applyid +"'";
            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionString,
                CommandType.Text, query);
            return dr;
        }

        public SqlDataReader GetApplicantByApplyidEdit(string applyid) {
            string query = "SELECT * FROM Applicant WHERE app_applyid='" + applyid +"' And app_title!='申请人'";
            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionString,
                CommandType.Text, query);
            return dr;
        }
    }
}